package mo.tags.stack;

import java.util.Arrays;
import java.util.Comparator;

public class L853 {

    public int carFleet(int target, int[] position, int[] speed) {
        int n = position.length;
        Car[] cars = new Car[n];
        for (int i = 0; i < n; i++) {
            cars[i] = new Car(position[i], (double) (target-position[i])/speed[i]);
        }
        Arrays.sort(cars, Comparator.comparingInt(o -> o.position));
        int ans = 0;
        int t = n;
        while (--t > 0) {
            if (cars[t-1].time > cars[t].time) {
                ans++;
            } else {
                cars[t-1] = cars[t];
            }
        }
        ans++;
        return ans;
    }

    private static class Car {
        private int position;
        private double time;

        public Car(int position, double time) {
            this.position = position;
            this.time = time;
        }
    }
}
